package com.student.dong.controller;


import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.student.dong.domain.AlgsStudent;
import com.student.dong.entity.AlgsEvaluate;
import com.student.dong.entity.AlgsLabReport;

import com.student.dong.service.IAlgsStudentService;
import com.student.dong.service.IEvaluateService;
import com.student.dong.service.LabReportService;
import com.student.dong.utils.Result;
import com.student.dong.vo.CurriculaVaribleVo;

import io.lettuce.core.dynamic.annotation.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

import static com.ruoyi.common.core.utils.PageUtils.startPage;
import static com.ruoyi.common.core.web.domain.AjaxResult.success;


/**
 * @Program: animalSystem->EvaluateController
 * @Description:
 * @Author: donghang
 * @Date: 2023-03-23 15:11
 * @Version： 1.0
 **/
@RestController
@RequestMapping("/homework")
public class EvaluateController {

    @Autowired
    private IEvaluateService evaluateService;

    @Autowired
    private LabReportService labReportService;


    @Autowired
    private IAlgsStudentService studentService;

    @GetMapping("/allHomework")
    public Result getDataTable() {
        Long userId = SecurityUtils.getUserId();
        List<CurriculaVaribleVo> list = evaluateService.selHomeWork(userId);
        return Result.ok().data("list",list);
    }


    @GetMapping( "/selOne/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        Long userId = SecurityUtils.getUserId();
        AlgsStudent algsStudent = studentService.selectByUserId(userId);
        Long studentId = algsStudent.getStudentId();
        return success(evaluateService.selOne(id,studentId));
    }

    /**
     * 提交作业
     */
    //@RequiresPermissions("student:student:edit")
    @Log(title = "(注册)学生信息", businessType = BusinessType.UPDATE)
    @PutMapping("/update")
    public AjaxResult edit(@RequestBody CurriculaVaribleVo curriculaVaribleVo)
    {
        AlgsLabReport algsLabReport = new AlgsLabReport();
        algsLabReport.setTeacherId(curriculaVaribleVo.getTeacherId());
        algsLabReport.setStudentId(curriculaVaribleVo.getStudentId());
        algsLabReport.setLabReportContent(curriculaVaribleVo.getLabReportContent());
        algsLabReport.setCurriculaVaribleId(curriculaVaribleVo.getCurriculaVaribleId());
        algsLabReport.setTypeId(7);
        labReportService.insert(algsLabReport);
        return AjaxResult.success();
    }



    /**
     * 教师评分
     */
    //@RequiresPermissions("student:student:edit")
    @Log(title = "(注册)学生信息", businessType = BusinessType.UPDATE)
    @PutMapping("/updateScore")
    public AjaxResult score(@RequestBody CurriculaVaribleVo curriculaVaribleVo)
    {

        Integer teacherId = curriculaVaribleVo.getTeacherId();
        Integer studentId = curriculaVaribleVo.getStudentId();
        Integer curriculaVaribleId = curriculaVaribleVo.getCurriculaVaribleId();
        AlgsEvaluate algsEvaluate = evaluateService.selKey(studentId.longValue(), teacherId.longValue(),curriculaVaribleId.longValue());
        //成绩
        Double labReportNumber = curriculaVaribleVo.getLabReportNumber();
        //教师评价
        String evaluateText = curriculaVaribleVo.getEvaluateText();
        algsEvaluate.setEvaluateGrade(BigDecimal.valueOf(labReportNumber));
        algsEvaluate.setEvaluateText(evaluateText);
        evaluateService.updateByPrimaryKeySelective(algsEvaluate);

        AlgsLabReport algsLabReport = labReportService.selKey(studentId.longValue(), teacherId.longValue(),curriculaVaribleId.longValue());
        algsLabReport.setLabReportNumber(BigDecimal.valueOf(labReportNumber));
        algsLabReport.setTypeId(8);
        labReportService.updateByPrimaryKeySelective(algsLabReport);
        return AjaxResult.success();
    }
}
